<template>
  <el-dialog
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    @close="close"
  >
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="roleIds">
      <!-- 多选的选项 -->
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="addRole">确定</el-button>
      <el-button size="small" @click="$emit('update:showRoleDialog')">
        取消
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting.js'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employee'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      roleList: [],
      roleIds: [],
      currUserID: ''
    }
  },
  created () {
    this.getRoleList()
  },
  methods: {
    // 确定添加角色
    async addRole () {
      await assignRoles({ id: this.currUserID, roleIds: this.roleIds })
      this.$message.success('分配成功')
      this.$emit('update:showRoleDialog')
    },
    // 根据id获取用户详细信息
    async getUserDetailById (id) {
      const detail = await getUserDetailById(id)
      // 存储当前用户id
      this.currUserID = id
      this.roleIds = detail.roleIds
      console.log('用户详情', detail)
    },
    close () {
      this.$emit('update:showRoleDialog')
    },
    async getRoleList () {
      const { rows } = await getRoleList({ page: 1, pagesize: 1000 })
      this.roleList = rows
    }
  }
}
</script>
